package institution

import (
	"net/http"

	"github.com/EDDYCJY/go-gin-example/pkg/app"
	"github.com/EDDYCJY/go-gin-example/pkg/e"
	"github.com/EDDYCJY/go-gin-example/pkg/util"
	"github.com/EDDYCJY/go-gin-example/service/institution_service"
	"github.com/gin-gonic/gin"
	"github.com/unknwon/com"
)

type GetTagVerify struct {
}

func GetTag(c *gin.Context) {
	var (
		appG = app.Gin{C: c}
		form GetTagVerify
	)
	_, errCode := app.BindAndValidMsg(c, &form)
	if errCode != nil {
		appG.ResponseMsg(errCode, nil)
		return
	}

	InstitutionID := c.MustGet("institution_id").(int)
	Service := institution_service.GetTag{
		InstitutionID: InstitutionID,
		PageNum:       util.GetPage(c),
		PageSize:      util.GetLimit(c),
	}
	data, err := Service.GetTag()
	if err != nil {
		appG.ResponseMsg(err, nil)
		return
	}
	//异步日志
	appG.Response(http.StatusOK, e.SUCCESS, data)

}

type AddTagVerify struct {
	TagName string `alias:"标签" form:"tag_name"  json:"tag_name" `
}

func AddTag(c *gin.Context) {
	var (
		appG = app.Gin{C: c}
		form AddTagVerify
	)
	_, errCode := app.BindAndValidMsg(c, &form)
	if errCode != nil {
		appG.ResponseMsg(errCode, nil)
		return
	}

	InstitutionID := c.MustGet("institution_id").(int)
	Service := institution_service.AddTag{
		InstitutionID: InstitutionID,
		TagName:       form.TagName,
	}
	data, err := Service.AddTag()
	if err != nil {
		appG.ResponseMsg(err, nil)
		return
	}
	//异步日志
	appG.Response(http.StatusOK, e.SUCCESS, data)

}

type EditTagVerify struct {
	ID      int    `form:"id" valid:"Required;"`
	TagName string `alias:"标签" form:"tag_name"  json:"tag_name" `
}

func EditTag(c *gin.Context) {
	var (
		appG = app.Gin{C: c}
		form = EditTagVerify{ID: com.StrTo(c.Param("id")).MustInt()}
	)
	_, errCode := app.BindAndValidMsg(c, &form)
	if errCode != nil {
		appG.ResponseMsg(errCode, nil)
		return
	}

	InstitutionID := c.MustGet("institution_id").(int)
	Service := institution_service.EditTag{
		TagID:         form.ID,
		InstitutionID: InstitutionID,
		TagName:       form.TagName,
	}
	data, err := Service.EditTag()
	if err != nil {
		appG.ResponseMsg(err, nil)
		return
	}
	//异步日志
	appG.Response(http.StatusOK, e.SUCCESS, data)

}

type DeleteTagVerify struct {
	ID int `form:"id" valid:"Required;"`
}

func DeleteTag(c *gin.Context) {
	var (
		appG = app.Gin{C: c}
		form = DeleteTagVerify{ID: com.StrTo(c.Param("id")).MustInt()}
	)
	_, errCode := app.BindAndValidMsg(c, &form)
	if errCode != nil {
		appG.ResponseMsg(errCode, nil)
		return
	}

	InstitutionID := c.MustGet("institution_id").(int)
	Service := institution_service.DeleteTag{
		TagID:         form.ID,
		InstitutionID: InstitutionID,
	}
	data, err := Service.DeleteTag()
	if err != nil {
		appG.ResponseMsg(err, nil)
		return
	}
	//异步日志
	appG.Response(http.StatusOK, e.SUCCESS, data)

}
